﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace Lawn.AppServer
{
    public class DefaultTopicManager:ITopicManager
    {

        private List<OptionsRemoteServer> options;
        public DefaultTopicManager(IOptions<List<OptionsRemoteServer>> options)
        {
            this.options = options.Value;
        }

        public async Task<IEnumerable<TopicType>> GetTopicByKey(string key)
        {
            return await Task.FromResult(options.Where(x=>x.Key==key).SelectMany(x => x.TopicTypes));
        }

        

        public async Task<TopicType> GetTopic(string key,string name)
        {
            var server = options.FirstOrDefault(x => x.Key == key);

            var topicType= server?.TopicTypes.Where(x => x.Topic == name)?.FirstOrDefault();

            return  await Task.FromResult(topicType);
        }

        public async Task<IEnumerable<TopicType>> GetAll()
        {
 
           return await Task.FromResult(options.SelectMany(x => x.TopicTypes));
        }
    }
}